
var cancalDistance = 120;
function fnLongPressRecording() {
	//录音按钮
	var bt_recoding = document.getElementById("bt_recoding");

	//中间黑色边框和里面的内容（录音状态）
	var blackBoxSpeak = document.querySelector(".blackBoxSpeak");
	blackBoxSpeak.style.background = "url('../../image/im/ic_record.png') no-repeat 28 16px/65px 104px, url('../../image/im/ic_record_ripple_9.png')no-repeat 111.2px 32px/28.8px 88px";
	blackBoxSpeak.style.backgroundColor = "rgba(0,0,0,.7)";

	//中间黑色边框和里面的内容（暂停状态）
	var blackBoxPause = document.querySelector(".blackBoxPause");
	blackBoxPause.style.background = "rgba(0,0,0,.7) url('../../image/im/ic_release_to_cancel.png')no-repeat center 8px/67.2px 104px";
	blackBoxPause.style.backgroundColor = "rgba(0,0,0,.7)";

	//手指移动相关
	var posStart = 0; //初始化起点坐标
	var posEnd = 0; //初始化终点坐标
	var posMove = 0; //初始化滑动坐标

	//轮播相关
	var index = [9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9];
	var num = index.length;
	var timer = null; //用于清除计时器
	var recorder = api.require('recMp3');

	function initEvent() {
		bt_recoding.addEventListener("touchstart", function(event) {
			setTimer();
			event.preventDefault(); //阻止浏览器默认行为
			posStart = 0;
			posStart = event.touches[0].pageY; //获取起点坐标
			recorder.start(function(ret, err) {
				if(!ret) {
					console.error('打开录音失败>>>>'+ err.message);
				}
			});
			//显示录音 隐藏暂停
			showBlackBoxSpeak();
		});
		bt_recoding.addEventListener("touchmove", function(event) {
			event.preventDefault(); //阻止浏览器默认行为

			posMove = event.targetTouches[0].pageY; //获取滑动实时坐标
			if(posStart - posMove < cancalDistance) {
				//隐藏录音 显示暂停
				showBlackBoxSpeak();
			} else {
				//显示录音 隐藏暂停
				showBlackBoxPause();
			}
		});
		bt_recoding.addEventListener("touchend", function(event) {
			event.preventDefault(); //阻止浏览器默认行为
			posEnd = 0;
			posEnd = event.changedTouches[0].pageY; //获取终点坐标
			
			clearInterval(timer);
			//初始化状态
			initStatus();

			if(posStart - posEnd < cancalDistance) {
				console.log("发送录音");
				recorder.stop(function(ret) {
					// 时长
					var duration = ret.duration;
					if(duration == 0) {
						fnToast('录音时间太短');
					}else {
						// 音频路径
				        var path = ret.path;
				        api.sendEvent({
				        	name: 'imRecorderSend',
				        	extra: {
				        		duration: duration,
				        		path: path
				        	}
				        })
					}
				});
				showBlackBoxNone();
			} else {
				console.log("录音取消发送");
				recorder.stop();
				showBlackBoxNone();
			}
		});
	}

	initEvent();

	//轮播
	function setTimer() {
		timer = setInterval(function() {
			setTimeout(function() {
				num++;
				blackBoxSpeak.style.background = "url('../../image/im/ic_record.png')no-repeat 28px 16px/64px 104px, url('../../image/im/ic_record_ripple_" + index[num] + ".png')no-repeat 111.2px 32px/28.8px 88px";
				blackBoxSpeak.style.backgroundColor = " rgba(0,0,0,.7)";
			}, 70);
			if(num >= index.length - 1) {
				num = 0;
			}
		}, 200);
	}

	//初始化状态
	var initStatus = function() {
		bt_recoding.value = '按住 说话';
		
		//全部隐藏
		showBlackBoxNone();
	}

	//显示录音 隐藏暂停
	var showBlackBoxSpeak = function() {
		bt_recoding.value = '松开 结束';
		blackBoxSpeak.style.display = "block";
		blackBoxPause.style.display = "none";
	}

	//隐藏录音 显示暂停
	var showBlackBoxPause = function() {
		bt_recoding.value = '松开手指，取消发送';
		blackBoxSpeak.style.display = "none";
		blackBoxPause.style.display = "block";
	}

	//隐藏录音
	var showBlackBoxNone = function() {
		blackBoxSpeak.style.display = "none";
		blackBoxPause.style.display = "none";
	}
}